package licode.unisop.authcenter.adapter.web;

import licode.unisop.authcenter.application.executor.CenterRealmUserExecutor;
import licode.unisop.authcenter.application.info.RealmUserGroupsRemoveFromUserIn;
import licode.unisop.authcenter.application.info.RealmUsersJoinToGroupIn;
import licode.unisop.client.inter.HasAuthority;
import licode.unisop.provider.info.*;
import licode.unisop.provider.vo.SiAccountSet;
import licode.unisop.realm.client.info.SiResetPasswordIn;
import licode.unisop.realm.client.info.SiUpdatePasswordIn;
import licode.unisop.types.AuthConst;
import licode.unisop.unilite.result.UniReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 定义统一认证平台中心领域相关接口
 *
 * @author licode
 */
@Slf4j
@UniReturnResult
@RestController
@RequestMapping("/uni-center/v1/realm")
public class CenterRealmUserController {
    @Resource
    private CenterRealmUserExecutor centerRealmUserExecutor;

    @PostMapping("/user/cache/update")
    public void updateCache(@RequestBody SiUserId userId) {
        centerRealmUserExecutor.updateCache(userId);
    }

    /**
     * 修改用户密码
     * @param info 账号信息
     */
    @RequestMapping(value = "/user/auth/password/update", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "password:reset")
    public void updatePassword(@RequestBody SiUpdatePasswordIn info) {
        centerRealmUserExecutor.updatePassword(info);
    }

    /**
     * 重置用户密码
     * @param info 账号信息
     */
    @RequestMapping(value = "/user/auth/password/reset", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "password:reset")
    public void resetPassword(@RequestBody SiResetPasswordIn info) {
        centerRealmUserExecutor.resetPassword(info);
    }

    /**
     * 保存用户的账号信息
     * @param info 账号信息
     */
    @RequestMapping(value = "/user/account/save", method = RequestMethod.POST)
    @HasAuthority(roles= AuthConst.SYSTEM_ADMIN, permits = "account:update")
    public void saveAccount(@RequestBody SiAccountSet info) {
        centerRealmUserExecutor.saveAccount(info);
    }

    /**
     * 把领域用户分组分配给平台
     */
    @RequestMapping(value = "/user/group/assign-platform/to-group", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void assignPlatsToUserGroup(@RequestBody SiPlatsAssignToUserGroupIn req) {
        centerRealmUserExecutor.assignPlatsToUserGroup(req);
    }

    /**
     * 把领域用户分组分配给平台
     */
    @RequestMapping(value = "/user/group/assign-group/to-platform", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void assignGroupsToPlatform(@RequestBody SiUserGroupsAssignToPlatIn req) {
        centerRealmUserExecutor.assignGroupsToPlatform(req);
    }

    /**
     * 把领域用户分组分配给平台
     */
    @RequestMapping(value = "/user/group/of-platform/save", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void saveGroupsOfPlatform(@RequestBody SiUserGroupsAssignToPlatIn req) {
        centerRealmUserExecutor.saveGroupsOfPlatform(req);
    }

    /**
     * 移除分配给平台的领域用户分组
     */
    @RequestMapping(value = "/user/group/assign-platform/remove", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void removeGroupFromPlatform(@RequestBody UserGroupRemoveFromPlatIn req) {
        centerRealmUserExecutor.removeGroupFromPlatform(req);
    }

    /**
     * 把用户加入指定用户分组
     */
    @RequestMapping(value = "/user/join-to-group", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void userJoinToGroup(@RequestBody RealmUsersJoinToGroupIn req) {
        centerRealmUserExecutor.userJoinToGroup(req);
    }

    /**
     * 把领域用户分组分配给指定用户
     */
    @RequestMapping(value = "/group/assign-to-user", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void groupAssignToUser(@RequestBody SiGroupAssignToUsersIn req) {
        centerRealmUserExecutor.groupAssignToUser(req);
    }

    /**
     * 把用户从分组中移除
     */
    @RequestMapping(value = "/user/remove-from-group", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void userRemoveFromGroup(@RequestBody UsersRemoveFromGroupIn req) {
        centerRealmUserExecutor.userRemoveFromGroup(req);
    }

    /**
     * 把分组列表从指定用户中移除
     */
    @RequestMapping(value = "/group/remove-from-user", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void groupRemoveFromUser(@RequestBody RealmUserGroupsRemoveFromUserIn req) {
        centerRealmUserExecutor.groupRemoveFromUser(req);
    }
}
